import { loginApi } from "@/api/login";
import { getToken, removeToken, setToken } from "@/request/token";

// 引入存储token的方法
export default {
    namespaced: true,
    state: {
        token: getToken(),
        isCollapse: false
    },
    mutations: {
        iscollapse(state) {
            state.isCollapse = !state.isCollapse
        },
        // 修改token
        setToken(state, token) {
            state.token = token
            setToken(token)
        },
        // 删除tokne
        removeToken(state) {
            state.token = null
            removeToken()
        }
    },
    actions: {
        // 调用登录api存储token
        async loginActions({ commit }, user) {
            let res = await loginApi(user)
            commit('setToken', res.token)
        },
        // 退出登录
        logOutActions({ commit }) {
            commit('removeToken')
        }
    }
}